package entity;
import com.hzheima.util.ImageUtil;
import constant.BallConstant;
import constant.UiConstant;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.Objects;
import java.util.Random;

/**
 * @Author:zhengbowen
 * @Date: 2025/09/07 18:28
 * @Description:球类
 */
public class Ball extends FlyingObject implements Cloneable{


    private int xSpeed; //水平方向的速度
    private int ySpeed;//竖直方向的速度
    private int startLoc;//初始位置
    private boolean startDown;
    public static ArrayList<Ball> balls = new ArrayList<>();//球集合
    private Random random = new Random();
    private int blood;//血量
    private int currentBlood;
    private static int count = 15;

    boolean spitFlag=false;//小球分裂标志
    public Ball() {
        //随机图片
        int i = random.nextInt(2)+1;
        image = ImageUtil.getImg("c" + i + ".png");
        //设置血量
        blood = random.nextInt(13) + 1;
        currentBlood = blood;
        //设置宽高
        width = (int) (image.getWidth() * ((0.3 + blood * 0.05)));
        height = (int) (image.getHeight() * ((0.3 + blood * 0.05)));
//        width=image.getWidth();
//        height=image.getHeight();
        //随机位置
        startLoc = random.nextInt(2);
        //设置x,y
        if (startLoc == BallConstant.LEFT) {
            //随机生成在左边
            x = -width;
            //设置速度
            xSpeed = 5;

        } else if (startLoc == BallConstant.RIGHT) {
            //随机生成在右边
            x = UiConstant.APP_WIDTH;
            //设置速度
            xSpeed = -5;
        }
        y = 100;
        ySpeed = 5;

    }
    //拷贝
    @Override
    protected Ball clone() throws CloneNotSupportedException {
        try {
            return (Ball) super.clone();
        } catch (CloneNotSupportedException e) {
            throw new AssertionError(); // 不会发生
        }
    }

    public void move() {

        //水平移动
        this.x += xSpeed;
        //如果移动到屏幕一半左右就能有向下的速度
        if (startLoc == BallConstant.LEFT) {
            //说明是从左边出来的
            if (x >= 250) {
                //可以向下移动了
                startDown = true;
            }
        } else if (startLoc == BallConstant.RIGHT) {
            //说明是从右边出来的
            if (x <= 250) {
                //可以向下移动了
                startDown = true;
            }
        }
        if (startDown) {
            this.y += ySpeed;
        }
    }

    public static void createBall() {
        if (balls.size() < BallConstant.BALL_MAXNUM) {
            count++;
            if (count % 5 == 0) {
                Ball ball = new Ball();
                balls.add(ball);
            }
        }
    }

    //小球移动
    public static void ballsMove() {
        for (int i = 0; i < balls.size(); i++) {
            Ball ball = balls.get(i);
            ball.move();
        }
    }

    //小球撞墙反弹
    public void rebound() {
        if (xSpeed > 0 && x > UiConstant.APP_WIDTH - width) {
            xSpeed = -xSpeed;
        }
        if (xSpeed < 0 && x < 0) {
            xSpeed = -xSpeed;
        }
        if (ySpeed > 0 && y > UiConstant.APP_HEIGHT - height) {
            ySpeed = -ySpeed;
        }
        if (ySpeed < 0 && y < 0) {
            ySpeed = -ySpeed;
        }
    }
    //绘制小球血量

    @Override
    public void draw(Graphics p) {
        super.draw(p);
        //绘制血量
        if (currentBlood >= 0)
            p.setColor(Color.red);
        if (currentBlood >= 5)
            p.setColor(Color.orange);
        if (currentBlood >= 10)
            p.setColor(Color.green);
        p.setFont(new Font("楷体", Font.BOLD, 30));
        int dx = blood >= 10 ? 15 : 10;
        p.drawString(String.valueOf(currentBlood), this.x + this.width / 2 - dx, this.y + this.height / 2 + 10);
    }

    public boolean getSpitFlag() {
        return spitFlag;
    }

    public void setSpitFlag(boolean spitFlag) {
        this.spitFlag = spitFlag;
    }

    public int getCurrentBlood() {
        return currentBlood;
    }

    public void setCurrentBlood(int currentBlood) {
        this.currentBlood = currentBlood;
    }

    @Override
    public int getxSpeed() {
        return xSpeed;
    }

    @Override
    public void setxSpeed(int xSpeed) {
        this.xSpeed = xSpeed;
    }

    public int getBlood() {
        return blood;
    }

    public void setBlood(int blood) {
        this.blood = blood;
    }

    @Override
    public int getySpeed() {
        return ySpeed;
    }

    @Override
    public void setySpeed(int ySpeed) {
        this.ySpeed = ySpeed;
    }

    //判断子弹与球的碰撞逻辑
    public boolean shootBy(Bullet bullet) {
        //      shootBy 可以判断是否和子弹碰撞了，如果碰撞了，血量减少
        boolean flag = bullet.y < this.y + this.height && bullet.y + bullet.height > this.y
                && bullet.x + bullet.width > this.x && bullet.x < this.x + this.width;
        if (flag) {
            //血量减少时,大小改变
            this.currentBlood-=bullet.getDamage();
            width = (int) (image.getWidth() * ((0.3 + currentBlood * 0.05)));
            height = (int) (image.getHeight() * ((0.3 + currentBlood * 0.05)));
            startDown = true;
        }
        return flag;
    }
    //小球分裂,只分裂一次
    public void split(){
        try {
            Ball newBall =this.clone();
            // 先获取xSpeed的绝对值，确保nextInt参数为正
            int absXSpeed = Math.abs(this.xSpeed);
            int absYSpeed = Math.abs(this.ySpeed);
            int newXSpeed;
            int newYSpeed;
            if (absXSpeed == 0) {
                // 如果xSpeed为0，设置一个默认的最小速度
                newXSpeed = random.nextBoolean() ? 1 : -1;
            } else {
                // 生成[1, absSpeed]范围内的随机数
                int magnitudeX = random.nextInt(absXSpeed) + 1;
                // 与父球X方向相反
                newXSpeed = (this.xSpeed > 0) ? -magnitudeX : magnitudeX;
            }
            if (absYSpeed == 0) {
                newYSpeed = random.nextBoolean() ? 1 : -1;
            } else {
                int magnitudeY = random.nextInt(absYSpeed) + 1;
                // 与父球Y方向相反
                newYSpeed = (this.ySpeed > 0) ? -magnitudeY : magnitudeY;
            }
            newBall.setxSpeed(newXSpeed);
            newBall.setySpeed(newYSpeed);
            this.setxSpeed(-newXSpeed);
            this.setySpeed(-newYSpeed);
            //新产生的小球不能再分裂了
            newBall.setSpitFlag(true);
            balls.add(newBall);
        } catch (CloneNotSupportedException e) {
            System.out.println("拷贝失败");
        }
    }
}
